package com.yjxxt;

import com.yjxxt.dao.IDeptDao;
import com.yjxxt.dao.IEmpDao;
import com.yjxxt.vo.Dept;
import com.yjxxt.vo.Emp;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * packageName com.yjxxt
 *
 * @author TongHao
 * @date 2025/8/4
 * @description TODO
 */

public class Main {

    static SqlSessionFactory sqlSessionFactory = null; //Mybatis框架的核心接口创建sqlsession对象
    static SqlSession session = null;
    static {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try{
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }



    public static void main(String[] args) throws IOException {
//        selectByDnameAndEname();                       //    a.查找所有部门的名称和部门领导的方法,返回一个集合
//        System.out.println("=========================");
//        selectByMaxDept();                             // * b.查找部门人数最多的方法,要求返回该部门的详细信息
//        System.out.println("=========================");
//        addDept();                                      // * c.增加一个部门及领导的方法 ，返回boolean
//        System.out.println("=========================");
//        selectAllDeptAndEmp();                          //a.查找所有员工及部门名称的方法，返回一个集合   *
//        selectMaxSalByEname();                          //b.查找工资最高员工的方法，返回一个对象    *
//        updateEmp();                                    //c.修改员工的方法              *
//        selectByEmpno();                                //d.查找一个员工的方法           *
//        deleteByEmpno();                                //e.根椐ID删除员工的方法         *
//          addByEmpno();                                 //g.增加一个员工，并返回其ID     *
        selectByEname();                                //f.根椐名字中的任意一个字查找的方法   *
    }


    public static void testInsert() {
        session = sqlSessionFactory.openSession(false);

        IDeptDao deptDao = session.getMapper(IDeptDao.class);
//        Dept dept = deptDao.selectByDeptNo(10);
//        Dept dept = deptDao.selectByUsernameAndPassword("ACCOUNTING",10);
//        List<Dept> dept = deptDao.findByPage(1,4);
        try{
//            int row = deptDao.insert(new Dept(50,"大數據測試","上海"));
//            System.out.println(row);
            //手動提交事物
            session.commit();
        }catch (Exception e){
            session.rollback();
        }finally {
            if (session != null){
                session.close();
            }
        }
    }
    public static void selectByDnameAndEname() {
        session = sqlSessionFactory.openSession();
        IDeptDao deptDao = session.getMapper(IDeptDao.class);
        List<Map<String, String>> depts = deptDao.selectByDnameAndEname();
        System.out.println(depts);
    }

    public static void selectByMaxDept() {
        session = sqlSessionFactory.openSession();
        IDeptDao deptDao = session.getMapper(IDeptDao.class);
        List<Emp> emps = deptDao.selectByMaxDept();
        System.out.println(emps);
    }

    public static void addDept() {
        session = sqlSessionFactory.openSession(false);
        IDeptDao deptDao = session.getMapper(IDeptDao.class);
        try {
            boolean result = deptDao.addDept(new Dept(6, "测试部"));
            System.out.println(result?"添加成功":"添加失败");
            session.commit(); //提交事物
        }catch (Exception e){
            session.rollback(); //如果异常，则回滚事物
        }finally {
            session.close();
        }
    }

    public static void selectAllDeptAndEmp(){
        session = sqlSessionFactory.openSession();
        IEmpDao empDao = session.getMapper(IEmpDao.class);
        List<Emp> empList = empDao.selectAllDeptAndEmp();
        System.out.println(empList);
    }

    public static void selectMaxSalByEname(){
        session = sqlSessionFactory.openSession();
        IEmpDao empDao = session.getMapper(IEmpDao.class);
        Map<String,Integer> maxSal = empDao.selectMaxSalByEname();
        System.out.println(maxSal);
    }

    public static void updateEmp(){
        session = sqlSessionFactory.openSession(false);
        IEmpDao empDao = session.getMapper(IEmpDao.class);
        try{
            int emp = empDao.updateEmp("张飞", "研发部长", 20000, 500, 101);
            System.out.println(emp);
            session.commit();
        }catch (Exception e){
            session.rollback();
        }finally {
            session.close();
        }
    }

    public static void selectByEmpno(){
        session = sqlSessionFactory.openSession();
        IEmpDao empDao = session.getMapper(IEmpDao.class);
        Emp emp = empDao.selectByEmpno(101);
        System.out.println(emp);
        session.close();
    }

    public static void deleteByEmpno(){
        session = sqlSessionFactory.openSession(false);
        IEmpDao empDao = session.getMapper(IEmpDao.class);
        try{
            int i = empDao.deleteByEmpno(101);
            session.commit();
            if (i>0){
                System.out.println(i);
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        }catch (Exception e){
            session.rollback();
        }finally {
            session.close();
        }
    }

    public static void addByEmpno() {
        session = sqlSessionFactory.openSession(false);
        IEmpDao empDao = session.getMapper(IEmpDao.class);
        try{
            int emp = empDao.addByEmpno(new Emp(101, "张飞", "程序员",
                    101, new Date(), 20000, 500, 1));
            if (emp > 0){
                session.commit();
                System.out.println("添加成功");
            }
        }catch (Exception e){
            session.rollback();
        }finally {
            session.close();
        }
    }

    public static void selectByEname(){
        session = sqlSessionFactory.openSession();
        IEmpDao dao = session.getMapper(IEmpDao.class);
        Emp emp = dao.selectByEname("飞");
        System.out.println(emp);
    }

    //删除所有部门员工和部门方法
    public static void DeleteByDeptAndEmp(){
        session = sqlSessionFactory.openSession();

    }
}
